/** 应用模型配置 */
import type { ApplicationModelIndicator } from '@/types/modules/application-models'
import { loadPointList } from '@/api/modules/indicator'
import { loadModelOfIndicator } from '@/api/modules/applicationModel'
import { getModes } from '@/mixins/StatisticalIndicators/MixinStatisticalIndicators'
import { STORAGE_TYPES } from '@/enum/modules/indicator'

export function useModelIndicators() {
  const [indicators, setIndicators] = useShallowState<
    ApplicationModelIndicator[]
  >([])
  const [indicatorLoading, setIndicatorLoading]
    = useState(false)
  const [modelIndicators, setModelIndicators]
    = useShallowState<ApplicationModelIndicator[]>([])
  const [modelIndicatorLoading, setModelIndicatorLoading]
    = useState(false)

  async function getIndicatorsOfPoint(
    pointId: number,
    needLoading: boolean = true,
  ) {
    needLoading && setIndicatorLoading(true)
    try {
      const { data } = await loadPointList(pointId, 0)
      setIndicators(
        (data || []).map(v => ({
          id: v.id!,
          indicatorId: v.id!,
          indicatorName: v.indicatorName,
          indicatorCode: v.indicatorCode,
          unit: v.unit,
          pointFullName: v.pointFullName,
          storageMode: {
            collect: getModes(
              STORAGE_TYPES.COLLECT,
              v.indicatorDataStoreCoreList || [],
            ),
            entry: getModes(
              STORAGE_TYPES.ENTRY,
              v.indicatorDataStoreCoreList || [],
            ),
            calculate: getModes(
              STORAGE_TYPES.CALCULATE,
              v.indicatorDataStoreCoreList || [],
            ),
            statistic: getModes(
              STORAGE_TYPES.STATISTIC,
              v.indicatorDataStoreCoreList || [],
            ),
          },
        })),
      )
    }
    catch (e) {
    }
    finally {
      needLoading && setIndicatorLoading(false)
    }
  }
  async function getIndicatorsOfModel(
    modelId: number,
    includeSub: boolean = false,
    needLoading: boolean = true,
  ) {
    needLoading && setModelIndicatorLoading(true)
    try {
      const { data } = await loadModelOfIndicator(
        modelId,
        includeSub,
      )
      let i = 1
      setModelIndicators(
        (data || []).map((v: any) => ({
          id: i++, // 没什么用，只为了和类型对上
          indicatorId: v.modelIndicatorId,
          indicatorName: v.indicatorName,
          indicatorCode: v.indicatorNumber, // !!!指标编码而不是测点点位号
          unit: v.indicatorUnit,
          applicationModelId: v.applicationModelId,
          pointFullName: v.pointFullName,
          storageMode: {
            collect: getModes(
              STORAGE_TYPES.COLLECT,
              v.indicatorDataStoreCoreList || [],
            ),
            entry: getModes(
              STORAGE_TYPES.ENTRY,
              v.indicatorDataStoreCoreList || [],
            ),
            calculate: getModes(
              STORAGE_TYPES.CALCULATE,
              v.indicatorDataStoreCoreList || [],
            ),
            statistic: getModes(
              STORAGE_TYPES.STATISTIC,
              v.indicatorDataStoreCoreList || [],
            ),
          },
        })),
      )
    }
    catch (e) {
    }
    finally {
      needLoading && setModelIndicatorLoading(false)
    }
  }

  async function confirmDel() {
    return ElMessageBox.confirm(
      '确认要解除选中的模型配置吗？',
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      },
    )
  }

  return {
    /** 可配置的指标列表 */
    indicators,
    indicatorLoading,
    setIndicators,
    setIndicatorLoading,

    /** 已配置的指标列表 */
    modelIndicators,
    modelIndicatorLoading,
    setModelIndicators,
    setModelIndicatorLoading,

    /** 获取模型指标 */
    getIndicatorsOfModel,
    /** 获取点位指标 */
    getIndicatorsOfPoint,

    confirmDel,
  }
}
